package com.heba.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.heba.Cfg;
import com.heba.bean.diary.ItemDiaryDetailObject;
import com.heba.bean.message.ItemMessageDetailObject;
import com.heba.bean.message.MessageChatType;
import com.heba.bean.product.ItemProductInfoObject;
import com.heba.bean.user.UserObject;
import com.heba.config.CommonConfig;
import com.heba.greenmulu.R;
import com.heba.greenmulu.businesslist.BusinessHomePageActivity;
import com.heba.greenmulu.businesslist.BusinessPersonActivity;
import com.heba.greenmulu.diary.ItemPersonHomePageActivity;
import com.heba.greenmulu.scan.ItemProductDetailActivity;
import com.heba.greenmulu.user.UserDiaryListActivity;
import com.heba.helper.DateHelper;
import com.heba.utils.CommonUtils;
import com.heba.utils.GlideUtil;

import java.util.List;

/**
 * 聊天讯息详情adapter
 * Created by jason on 2020/3/6.
 */
public class ItemMessageChatDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;
    private List<ItemMessageDetailObject> list;

    public ItemMessageChatDetailAdapter(Context context, List<ItemMessageDetailObject> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        switch (viewType) {
            case MessageChatType.TYPE_USER_LEFT:
                return new ItemMessageChatDetailAdapter.UserMsgHolder(inflater.inflate(R.layout.recycle_item_message_card_left_row, parent, false));
            case MessageChatType.TYPE_USER_RIGHT:
                return new ItemMessageChatDetailAdapter.UserMsgHolder(inflater.inflate(R.layout.recycle_item_message_card_right_row, parent, false));
            case MessageChatType.TYPE_PRODUCT_LEFT:
                return new ItemMessageChatDetailAdapter.ProductMsgHolder(inflater.inflate(R.layout.recycle_item_message_product_left_row, parent, false));
            case MessageChatType.TYPE_PRODUCT_RIGHT:
                return new ItemMessageChatDetailAdapter.ProductMsgHolder(inflater.inflate(R.layout.recycle_item_message_product_right_row, parent, false));
            case MessageChatType.TYPE_DIARY_LEFT:
                return new ItemMessageChatDetailAdapter.DiaryMsgHolder(inflater.inflate(R.layout.recycle_item_message_article_left_row, parent, false));
            case MessageChatType.TYPE_DIARY_RIGHT:
                return new ItemMessageChatDetailAdapter.DiaryMsgHolder(inflater.inflate(R.layout.recycle_item_message_article_right_row, parent, false));
            case MessageChatType.TYPE_TEXT_LEFT:
                return new ItemMessageChatDetailAdapter.TextHolder(inflater.inflate(R.layout.recycle_item_message_text_left_row, parent, false));
            case MessageChatType.TYPE_TEXT_RIGHT:
                return new ItemMessageChatDetailAdapter.TextHolder(inflater.inflate(R.layout.recycle_item_message_text_right_row, parent, false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        int type = getItemViewType(position);
        ItemMessageDetailObject object = list.get(position);
        switch (type) {
            case MessageChatType.TYPE_USER_LEFT:
            case MessageChatType.TYPE_USER_RIGHT:
                UserMsgHolder userMsgHolder = (UserMsgHolder) viewHolder;
                userMsgHolder.bindViewHolder(object.getTimeCreated(), object.getUser());
                /**
                 * 个人名片
                 */
                userMsgHolder.itemView.setOnClickListener(v -> {
                    if (object.getUser() == null)
                        return;
                    Intent intent ;
                    if (object.getUser().getAccountType() == 0){
                       intent = new Intent(context, ItemPersonHomePageActivity.class);
                    }else{
                        intent = new Intent(context, BusinessPersonActivity.class);
                    }
                    Bundle bundle = new Bundle();
                    bundle.putString(CommonConfig.SEND_USER_ID, object.getUser().getUserID());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                });
                break;
            case MessageChatType.TYPE_PRODUCT_LEFT:
            case MessageChatType.TYPE_PRODUCT_RIGHT:
                ProductMsgHolder productMsgHolder = (ProductMsgHolder) viewHolder;
                productMsgHolder.bindViewHolder(object.getTimeCreated(), object.getProduct());
                /**
                 * 产品页面
                 */
                productMsgHolder.itemView.setOnClickListener(v -> {
                    if (object.getProduct() == null)
                        return;
                    Intent intent = new Intent(context, ItemProductDetailActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putString(CommonConfig.PRODUCT_ID, object.getProduct().getProductID());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                });
                break;
            case MessageChatType.TYPE_DIARY_LEFT:
            case MessageChatType.TYPE_DIARY_RIGHT:
                DiaryMsgHolder diaryMsgHolder = (DiaryMsgHolder) viewHolder;
                diaryMsgHolder.bindViewHolder(object.getTimeCreated(), object.getDiary());
                /**
                 * 日记页面
                 */
                diaryMsgHolder.itemView.setOnClickListener(v -> {
                    if (object.getDiary() == null)
                        return;
                    Intent intent = new Intent(context, UserDiaryListActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putInt(CommonConfig.TYPE, 1);
                    bundle.putString(CommonConfig.DIARY_ID, object.getDiary().getDiaryID());
                    intent.putExtras(bundle);
                    context.startActivity(intent);
                    ((Activity) context).overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);
                });
                break;
            case MessageChatType.TYPE_TEXT_LEFT:
            case MessageChatType.TYPE_TEXT_RIGHT:
                /**
                 * 文本显示
                 */
                TextHolder textHolder = (TextHolder) viewHolder;
                textHolder.bindViewHolder(object.getTimeCreated(), object.getMessage());
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        ItemMessageDetailObject object = list.get(position);
        if (object.getSendUserID().equals(Cfg.UserID)) {
            //自己发送的讯息
            switch (object.getType()) {
                case 0:
                    return MessageChatType.TYPE_PRODUCT_RIGHT;
                case 1:
                    return MessageChatType.TYPE_USER_RIGHT;
                case 2:
                    return MessageChatType.TYPE_TEXT_RIGHT;
                case 3:
                    return MessageChatType.TYPE_DIARY_RIGHT;
                default:
                    return MessageChatType.TYPE_USER_RIGHT;
            }
        } else {
            //别人发送的讯息
            switch (object.getType()) {
                case 0:
                    return MessageChatType.TYPE_PRODUCT_LEFT;
                case 1:
                    return MessageChatType.TYPE_USER_LEFT;
                case 2:
                    return MessageChatType.TYPE_TEXT_LEFT;
                case 3:
                    return MessageChatType.TYPE_DIARY_LEFT;
                default:
                    return MessageChatType.TYPE_USER_LEFT;
            }
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * 用户名片讯息item
     */
    class UserMsgHolder extends RecyclerView.ViewHolder {

        TextView tv_time;
        ImageView iv_avatar;
        TextView tv_name;

        public UserMsgHolder(@NonNull View itemView) {
            super(itemView);
            iv_avatar = itemView.findViewById(R.id.iv_avatar);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_name = itemView.findViewById(R.id.tv_name);
        }

        private void bindViewHolder(String time, UserObject object) {
            if (null != object) {
                tv_time.setText(DateHelper.transferDateString(context, time));
                GlideUtil.loadImageWithPlaceHolderID(context, object.getIcon(), iv_avatar, R.drawable.icon_head_portrait);
                tv_name.setText(object.getAccount());
            }
        }
    }

    /**
     * 产品讯息item
     */
    class ProductMsgHolder extends RecyclerView.ViewHolder {

        TextView tv_time;
        ImageView iv_product;
        TextView tv_product;
        TextView tv_content;
        TextView tv_weight;

        public ProductMsgHolder(@NonNull View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_product = itemView.findViewById(R.id.iv_product);
            tv_product = itemView.findViewById(R.id.tv_product);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_weight = itemView.findViewById(R.id.tv_weight);
        }

        private void bindViewHolder(String time, ItemProductInfoObject object) {
            if (null != object) {
                tv_time.setText(DateHelper.transferDateString(context, time));
                GlideUtil.loadImageWithDefaultPlaceHolder(context, object.getIcon(), iv_product);
                tv_product.setText(object.getBrandName());
                tv_content.setText(object.getProductName());
                tv_weight.setText(String.format(context.getResources().getString(R.string.per_serving_size), object.
                        getPackageSize(), object.getPackageUnit(), CommonUtils.subZeroAndDot(object.getServingSize()), object.getServingSizeUnit()));
            }
        }

    }

    /**
     * 日记讯息item
     */
    class DiaryMsgHolder extends RecyclerView.ViewHolder {

        TextView tv_time;
        ImageView iv_diary;
        TextView tv_content;
        TextView tv_diary;

        public DiaryMsgHolder(@NonNull View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            iv_diary = itemView.findViewById(R.id.iv_article);
            tv_content = itemView.findViewById(R.id.tv_content);
            tv_diary = itemView.findViewById(R.id.tv_diary);
        }

        private void bindViewHolder(String time, ItemDiaryDetailObject object) {
            if (null != object) {
                tv_time.setText(DateHelper.transferDateString(context, time));
                GlideUtil.loadImageWithDefaultPlaceHolder(context, object.getImageUrl(), iv_diary);
                tv_content.setText(CommonUtils.getDecodeText(object.getContent()));
                tv_diary.setText(String.format(context.getResources().getString(R.string.message_send_diary_user), object.getAccount()));
            }
        }

    }

    /**
     * 文本item
     */
    class TextHolder extends RecyclerView.ViewHolder {

        TextView tv_time;
        TextView tv_text;

        public TextHolder(@NonNull View itemView) {
            super(itemView);
            tv_time = itemView.findViewById(R.id.tv_time);
            tv_text = itemView.findViewById(R.id.tv_text);

        }

        private void bindViewHolder(String time, String message) {
            tv_time.setText(DateHelper.transferDateString(context, time));
            tv_text.setText(CommonUtils.getDecodeText(message));
        }

    }


}
